package start

import (
	"log"

	"gitee.com/LangHuaHuachihua/blog-go/conf"
	"gitee.com/LangHuaHuachihua/blog-go/ioc"
	"gitee.com/LangHuaHuachihua/blog-go/logger"
	"gitee.com/LangHuaHuachihua/blog-go/protocol"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cobra"

	_ "gitee.com/LangHuaHuachihua/blog-go/apps" //注册所有实现
)

var Cmd = &cobra.Command{
	Use:   "start",
	Short: "vlog后端",
	Long:  `个人博客`,
	Run: func(cmd *cobra.Command, args []string) {
		// Do Stuff Her	e
		//配置加载
		_, err := conf.LoadYamlConfig()
		if err != nil {
			log.Fatal(err.Error())
		}
		log.Println(args)
		//路由加载and载入业务
		r := gin.Default()
		// //blog模块
		// blogserver := &impl.Impl{}
		// blogserver.Init()
		// //此处接口类型已经为指针，要返回接口类型的实例，默认就是接口类型
		// handler := api.NewHandler(blogserver)
		// //载入协议服务
		// handler.Registry(r)
		// httpServer := protocol.NewHttp(r)
		//业务模块全部初始化ioc
		logger.L().Debug().Msgf("controllers:%s", ioc.ShowApi())
		logger.L().Debug().Msgf("apis:%s", ioc.ShowConntrollers())
		err = ioc.InitController()
		if err != nil {
			log.Fatal(err.Error())
		}

		ioc.InitHttpApi(r)
		if err != nil {
			log.Fatal(err.Error())
		}
		//启动
		httpServer := protocol.NewHttp(r)
		err = httpServer.Start()
		if err != nil {
			log.Fatal(err.Error())
		}
	},
}
